var menu = {
	"org-manage": {
		"menu": [{
			"title": "百度",
			"icon": "&#xe62f;",
			"menu": [
				{
					"title": "搜索",
					"href": "http://www.baidu.com/"
				},
				{
					"title": "地图",
					"href": "http://map.baidu.com/"
				},
				{
					"title": "贴吧",
					"href": "http://tieba.baidu.com/"
				}
			]
		},{
			"title": "腾讯",
			"icon": "&#xe62f;",
			"menu": [
				{
					"title": "微信",
					"href": "http://weixin.qq.com/"
				},
				{
					"title": "QQ",
					"href": "http://vip.qq.com/?ADTAG=www.qq.com"
				},
				{
					"title": "电脑管家",
					"href": "https://guanjia.qq.com/main.html?ADTAG=tr.pcmgr.qqcom.QQCOM"
				}
			]
		},{
			"title": "阿里巴巴",
			"icon": "&#xe62f;",
			"menu": [
				{
					"title": "天猫",
					"href": "https://www.tmall.com"
				},
				{
					"title": "淘宝",
					"href": "https://www.taobao.com/"
				},
				{
					"title": "阿里云",
					"href": "https://www.aliyun.com"
				}
			]
		}
		]
	},
	"main-data": {
		"menu": [{
			"title": "新闻",
			"icon": "&#xe62f;",
			"menu": [
				{
					"title": "网易",
					"href": "http://www.163.com/"
				},
				{
					"title": "搜狐",
					"href": "http://www.sohu.com/"
				},
				{
					"title": "凤凰新闻",
					"href": "http://www.ifeng.com/"
				}
			]
		},{
			"title": "视频",
			"icon": "&#xe62f;",
			"menu": [
				{
					"title": "优酷",
					"href": "http://youku.com/"
				},
				{
					"title": "爱奇艺",
					"href": "http://www.iqiyi.com/"
				},
				{
					"title": "百度视频",
					"href": "http://v.baidu.com/"
				}
			]
		}]
	}
}
var mainPlatform = {

	init: function(){

		this.bindEvent();
		// this.render(menu['home']);
	},

	bindEvent: function(){
		var self = this;
		// 顶部大菜单单击事件
		$(document).on('click', '.pf-nav-item', function() {
            $('.pf-nav-item').removeClass('current');
            $(this).addClass('current');

            // 渲染对应侧边菜单
            var m = $(this).data('menu');
            self.render(menu[m]);
        });

        $(document).on('click', '.sider-nav li', function() {
            $('.sider-nav li').removeClass('current');
            $(this).addClass('current');
            $('iframe').attr('src', $(this).data('src'));
        });

        $(document).on('click', '.pf-logout', function() {
            layer.confirm('您确定要退出吗？', {
              icon: 4,
			  title: '确定退出' //按钮
			}, function(){
			  location.href= 'login.html'; 
			});
        });
        //左侧菜单收起
        $(document).on('click', '.toggle-icon', function() {
            $(this).closest("#pf-bd").toggleClass("toggle");
            setTimeout(function(){
            	$(window).resize();
            },300)
        });

        $(document).on('click', '.pf-modify-pwd', function() {
            $('#pf-page').find('iframe').eq(0).attr('src', 'backend/modify_pwd.html')
        });

        $(document).on('click', '.pf-notice-item', function() {
            $('#pf-page').find('iframe').eq(0).attr('src', 'backend/notice.html')
        });
	},
	render: function(menu){
		
		if(menu == null)
		{
			alert("请先定义菜单！，格式请参考menu.js");
			return;
		}
		
		var current,html='',menuLi='';
/*			
左侧菜单的HTML结构。
                  <li class="current">
                        <a href="javascript:;">
                            <span class="iconfont sider-nav-icon">&#xe620;</span>
                            <span class="sider-nav-title">供应商组织</span>
                            <i class="iconfont">&#xe642;</i>
                        </a>
                        <ul class="sider-nav-s">
                           <li class="active"><a href="javascript:void(0);" onClick="openTab('.easyui-tabs1','供应商组织1','http://www.baidu.com')">供应商组织1</a></li>
                           <li class=""><a href="javascript:void(0);" onClick="openTab('.easyui-tabs1','表单管理','providers1.html')">表单管理</a></li>
                           <li><a href="#">供应商组织2</a></li>
                           <li><a href="#">供应商组织3</a></li>
                           <li><a href="#">供应商组织4</a></li>
                        </ul>
                     </li>*/
		for(var i = 0, len = menu.menu.length; i < len; i++){
			    //alert("二级菜单="+menu.menu[i].icon);
				//console.log(menu.menu[i].menu);
				var current = (i == 0) ? ' class="current"' : '';
				var menuLi = '';
				html = html + '<li'+current+'><a href="javascript:;"><span class="iconfont sider-nav-icon">'+menu.menu[i].icon+'</span><span class="sider-nav-title">'+menu.menu[i].title+'</span><i class="iconfont">&#xe642;</i></a><ul class="sider-nav-s">';
						for(var m = 0, len2 = menu.menu[i].menu.length; m < len2; m++)
						{
							//alert("获取三级菜单");
							//alert("二级菜单="+menu.menu[i].title+"下的三级菜单="+menu.menu[i].menu[m].title);
							var active = (m == 0) ? ' class="active"' : '';
							menuLi = menuLi + '<li '+active+'><a href="javascript:void(0);" onClick="openTab(\'.easyui-tabs1\',\''+menu.menu[i].menu[m].title+'\',\''+menu.menu[i].menu[m].href+'\')">'+menu.menu[i].menu[m].title+'</a></li>';
						}
						html = html+menuLi+'</ul></li>';
		}
		$('#pf-sider').find(".sider-nav").html(html);
	}

};

mainPlatform.init();

$('.easyui-tabs1').tabs({
	tabHeight: 44,
	onSelect:function(title,index){
		var currentTab = $('.easyui-tabs1').tabs("getSelected");
		if(currentTab.find("iframe") && currentTab.find("iframe").size()){
			currentTab.find("iframe").attr("src",currentTab.find("iframe").attr("src"));
		}
	}
})
$(window).resize(function(){
	$('.tabs-panels').height($("#pf-page").height()-46);
	$('.panel-body').height($("#pf-page").height()-76)
}).resize();

var page = 0,
	pages = ($('.pf-nav').height() / 70) - 1;

if(pages === 0){
	$('.pf-nav-prev,.pf-nav-next').hide();
}
$(document).on('click', '.pf-nav-prev,.pf-nav-next', function(){


	if($(this).hasClass('disabled')) return;
	if($(this).hasClass('pf-nav-next')){
		page++;
		$('.pf-nav').stop().animate({'margin-top': -70*page}, 200);
		if(page == pages){
			$(this).addClass('disabled');
			$('.pf-nav-prev').removeClass('disabled');
		}else{
			$('.pf-nav-prev').removeClass('disabled');
		}

	}else{
		page--;
		$('.pf-nav').stop().animate({'margin-top': -70*page}, 200);
		if(page == 0){
			$(this).addClass('disabled');
			$('.pf-nav-next').removeClass('disabled');
		}else{
			$('.pf-nav-next').removeClass('disabled');
		}

	}
})


//点击左边菜单，在右侧TAB组件以iframe的方式打开href连接。
//调用方式参考本页182,183行。
function openTab(selector,title,href){
	var e = $(selector).tabs('exists',title);
	if(e){
		$(selector).tabs('select',title);
		var tab = $(selector).tabs('getSelected');
		$(selector).tabs('update',{
			tab:tab,
			options:{
				title:title,
				content:'<iframe name="indextab" scrolling="auto" src="'+href+'" frameborder="0" style="width:100%;height:100%;"></iframe>',
				closable:true,
				selected:true
			}
		});
	}else{
		$(selector).tabs('add',{
			title:title,
			content:'<iframe name="indextab" scrolling="auto" src="'+href+'" frameborder="0" style="width:100%;height:100%;"></iframe>',
			iconCls:'',
			closable:true
		});
	}
}

setTimeout(function(){
   $('.tabs-panels').height($("#pf-page").height()-46);
   $('.panel-body').height($("#pf-page").height()-76)
}, 200)